package cn.ffcs.uec.organization.component;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModelList;

import cn.ffcs.uec.organization.component.bean.UecOrgOrgsListboxExtBean;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年5月27日
 */
@Controller
@Scope("prototype")
public class UecOrgOrgsListboxExt extends Div implements IdSpace {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2987104426987882007L;

	private UecOrgOrgsListboxExtBean bean = new UecOrgOrgsListboxExtBean();
	/**
	 * zul.
	 */
	private static final String zul = "/pages_nmg/uec_organization/comp/uecOrgOrgs_listbox_ext.zul";

	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");

	private UecOrgOrgs uecOrgOrgs;
	
	/**
	 * 通过这个对象找到，这个对象的下级
	 */
	private UecOrgOrgs qryNextUesOrgOrgs;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOrgOrgsListboxExt() {
		// 1.绑定页面
		Executions.createComponents(zul, this, null);
		// 2.页面控件与java属性绑定
		Components.wireVariables(this, bean);
		// 3.绑定监听事件
		Components.addForwards(this, this, '$');
	}
	
	/**
	 * 初始化
	 */
	public void onCreate() {
		onQuery();
	}

	/**
	 * 触发选中事件
	 */
	public void onSelectRequest() throws Exception {
		if (bean.getUecOrgOrgsListbox().getSelectedCount() > 0) {
			uecOrgOrgs = (UecOrgOrgs) bean.getUecOrgOrgsListbox().getSelectedItem()
					.getValue();
			Events.postEvent("onSelectUecOrgOrgs", this, uecOrgOrgs);
		} else {
			Events.postEvent("onSelectNoUecOrgOrgs", this, null);
		}
	}
	
	/**
	 * 接收查询该组织下级的事件
	 * @param event
	 */
	public void onQryNextUecOrgOrgsResponse(ForwardEvent event) {
		uecOrgOrgs = (UecOrgOrgs) event.getOrigin().getData();
		if(uecOrgOrgs != null) {
			qryNextUesOrgOrgs = UecOrgOrgs.newInstance();
			qryNextUesOrgOrgs = uecOrgOrgs;
			bean.getHboxId().setVisible(false);
			onQueryNextUesOrgOrgs();
		}
	}
	/**
	 * 根据当前的组织，查到该组织的所有下级组织
	 */
	public void onQueryNextUesOrgOrgs() {
		queryNextOrganization();
	}
	
	/**
	 * 查询该组织的所有下级组织
	 */
	public void queryNextOrganization() {
		int activePage = bean.getUecOrgOrgsListboxPaging().getActivePage();
		int pageSize = bean.getUecOrgOrgsListboxPaging().getPageSize();
		PageInfo pageInfo = uecOrgOrgsManager.queryNextUecOrgOrgsByUecOrgOrgs(qryNextUesOrgOrgs, activePage + 1,
				pageSize);
		ListModelList dataList = new BindingListModelList(pageInfo.getDataList(), true);
		bean.getUecOrgOrgsListbox().setModel(dataList);
		bean.getUecOrgOrgsListboxPaging().setTotalSize(NumericUtil.nullToZero(pageInfo.getTotalCount()));
	}
	

	/**
	 * 查询
	 */
	public void onQuery() {
		uecOrgOrgs = new UecOrgOrgs();
		uecOrgOrgs.setName(bean.getName().getValue());
		uecOrgOrgs.setCode(bean.getCode().getValue());
		this.queryOrganization();
	}

	/**
	 * 查询
	 */
	public void queryOrganization() {
		int activePage = bean.getUecOrgOrgsListboxPaging().getActivePage();
		int pageSize = bean.getUecOrgOrgsListboxPaging().getPageSize();
		PageInfo pageInfo = uecOrgOrgsManager.queryOrganization(uecOrgOrgs, activePage + 1,
				pageSize);
		ListModelList dataList = new BindingListModelList(pageInfo.getDataList(), true);
		bean.getUecOrgOrgsListbox().setModel(dataList);
		bean.getUecOrgOrgsListboxPaging().setTotalSize(NumericUtil.nullToZero(pageInfo.getTotalCount()));
	}

	/**
	 * 重置
	 */
	public void onReset() {
		bean.getName().setValue(null);
		bean.getCode().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getUecOrgOrgsListbox());
		this.bean.getUecOrgOrgsListboxPaging().setTotalSize(1);
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecOrgOrgs = null;
	}

	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		if(null != qryNextUesOrgOrgs) {
			this.queryNextOrganization();
		}else {
			this.queryOrganization();
		}
	}
}
